<?php

namespace app\admin\controller;



use app\common\controller\BaseCommon;

class Base extends BaseCommon
{
    //保存分页参数
    protected $p = 1;
    //保存每页大小
    protected $pageSize = 10;

    protected $beforeActionList = [
        'checkRequiredParam',
        'checkPaginateParam',
        'checkAuthToken',
    ];

    public function __construct()
    {
        parent::__construct();

    }

    /**
     * 对sid进行校验(请求action之前校验)
     * @return boolean
     */
    protected function checkRequiredParam()
    {
        //对sid进行校验
        $param = [
            'sid' => $this->request->param('sid')
        ];
        $rules = [
            'sid' => ['require', 'number']
        ];
        $msg = [
            'sid' => ['require' => '店铺Id必须', 'number' => '店铺Id不合法']
        ];

        $result = $this->validate($param, $rules, $msg);
        if (true !== $result) {
            $this->apiResult('', 404, $result);
        }

    }

    /**
     * 对uid进行校验
     * @return boolean
     */
    protected function checkUIDParam()
    {
        //对uid进行校验
        $param = [
            'uid' => $this->request->param('uid')
        ];
        $rules = [
            'uid' => ['require', 'number']
        ];
        $msg = [
            'uid' => ['require' => '用户Id必须', 'number' => '用户Id不合法']
        ];

        $result = $this->validate($param, $rules, $msg);
        if (true !== $result) {
            $this->apiResult('', 404, $result);
        }

    }

    /**
     * 自动校验分页参数
     */
    protected function checkPaginateParam()
    {
        //当前分页
        if ($this->request->has('p')) {
            $p = $this->request->param('p');
            $error = '分页参数错误';
            if ($p < 1 || empty($p)) {
                $this->apiResult('', 404, $error);
            }

            $result = $this->validate(
                ['p' => $p],
                ['p' => ['require', 'number']],
                ['p' => ['require' => $error, 'number' => $error]]
            );
            if (true !== $result) {
                $this->apiResult('', 404, $result);
            }

            $this->p = intval($p);
        }
        //每页大小
        if ($this->request->has('pageSize')) {
            $pageSize = $this->request->param('pageSize');
            $error = '每页大小参数错误';
            if ($pageSize < 1 || empty($pageSize)) {
                $this->apiResult('', 404, $error);
            }

            $result = $this->validate(
                ['pageSize' => $pageSize],
                ['pageSize' => ['require', 'number']],
                ['pageSize' => ['require' => $error, 'number' => $error]]
            );
            if (true !== $result) {
                $this->apiResult('', 404, $result);
            }

            $this->pageSize = $pageSize;
        }
    }


    /**
     * 后台登录后，Token认证
     */
    protected function checkAuthToken()
    {
        $auth_token = $this->request->param('auth_token');
        $debug = $this->request->param('debug');
        if($debug != 'tangxiao'){
            if(!$auth_token){
                $this->apiResult('', 401, 'Token不能为空');
            }

            $info = db('login_token')->where(['auth_token' => $auth_token, 'status' => 1])->find();
            if(!$info){
                $this->apiResult('', 401, '无效的Token');
            }

            $effect_time = 24 * 3600;
            if(time() - $info['add_time'] > $effect_time){
                db('login_token')->where('auth_token', $auth_token)->setField('status', -1);
                $this->apiResult('', 401, 'Token已失效');
            }
        }

    }
}
